<?php

namespace app\erp\model;

use think\Model;

class UOrderdb extends Model
{
    protected $connection = 'database.utec_db';
    protected $pk = 'order_id';
    protected $name = 'order';
    public $param = array();
    protected $supplier_id = [594, 601, 918, 10324, 10325, 10396, 10466, 10635, 10636]; // 优士创新商业集团有限公司账号

    public function saveOrder()
    {
        $get_uorder = $this->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
        if (in_array($this->param['supplier_id'], $this->supplier_id)) {
            $orderdb = new Orderdb();
            $get_order = $orderdb->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
            if ($get_order) {
                // 同步订单
                $get_order = $get_order->hidden(['order_id'])->toArray();
                $get_order['create_time'] = strtotime($get_order['create_time']);
                $get_order['delete_time'] = $get_order['delete_time'] ? strtotime($get_order['delete_time']) : null;
                $get_order['update_time'] = $get_order['update_time'] ? strtotime($get_order['update_time']) : null;
                $get_order['confirm_time'] = $get_order['confirm_time'] ? strtotime($get_order['confirm_time']) : null;
                $get_order['push_supplier_time'] = $get_order['push_supplier_time'] ? strtotime($get_order['push_supplier_time']) : null;
                $get_order['shopping_time'] = $get_order['shopping_time'] ? strtotime($get_order['shopping_time']) : null;
                $get_order['receipt_time'] = $get_order['receipt_time'] ? strtotime($get_order['receipt_time']) : null;
                $get_order['cancel_time'] = $get_order['cancel_time'] ? strtotime($get_order['cancel_time']) : null;
                $get_order['over_time'] = $get_order['over_time'] ? strtotime($get_order['over_time']) : null;
                $get_order['pay_time'] = $get_order['pay_time'] ? strtotime($get_order['pay_time']) : null;
                $get_order['gys_sh_time'] = $get_order['gys_sh_time'] ? strtotime($get_order['gys_sh_time']) : null;
                $get_order['supplier_confirm_time'] = $get_order['supplier_confirm_time'] ? strtotime($get_order['supplier_confirm_time']) : null;
                $get_order['gys_fh_time'] = $get_order['gys_fh_time'] ? strtotime($get_order['gys_fh_time']) : null;
                $get_order['apply_time'] = $get_order['apply_time'] ? strtotime($get_order['apply_time']) : null;
                $get_order['apply_shehe_time'] = $get_order['apply_shehe_time'] ? strtotime($get_order['apply_shehe_time']) : null;
                $get_order['invoice_time'] = $get_order['invoice_time'] ? strtotime($get_order['invoice_time']) : null;
                $get_order['ruku_time'] = $get_order['ruku_time'] ? strtotime($get_order['ruku_time']) : null;
                $get_order['caigou_invoice_time'] = $get_order['caigou_invoice_time']?strtotime($get_order['caigou_invoice_time']):null;
                unset($get_order['supplier_id']);
                unset($get_order['supplier_rate']);
                unset($get_order['supplier_state']);

                // 设置合作单位
                // 设置合作单位
                if ($this->param['supplier_id'] == 594) {
                    $get_order['hezuodanwei'] = '优士';
                } elseif ($this->param['supplier_id'] == 601) {
                    $get_order['hezuodanwei'] = '卓品';
                } elseif ($this->param['supplier_id'] == 918) {
                    $get_order['hezuodanwei'] = '多米';
                } elseif ($this->param['supplier_id'] == 10324) {
                    $get_order['hezuodanwei'] = '东晟';
                } elseif ($this->param['supplier_id'] == 10325) {
                    $get_order['hezuodanwei'] = '君长';
                } elseif ($this->param['supplier_id'] == 10396) {
                    $get_order['hezuodanwei'] = '自营1';
                } elseif ($this->param['supplier_id'] == 10466) {
                    $get_order['hezuodanwei'] = '自营2';
                } elseif ($this->param['supplier_id'] == 10635) {
                    $get_order['hezuodanwei'] = '自营A';
                } elseif ($this->param['supplier_id'] == 10636) {
                    $get_order['hezuodanwei'] = '自营B';
                } else {
                    $get_order['hezuodanwei'] = '泰龙吉';
                }

                $get_u_order = $this->useGlobalScope(false)->where('order_no', $get_order['order_no'])->find();
                if ($get_u_order) {
                    $get_order['delete_time'] = null;
                    //    $get_order['cancel_type'] = 0;
                    $get_u_order->save($get_order);
                } else {
                    $get_order['supplier_id'] = 0;
                    $get_order['supplier_rate'] = 0;
                    $get_order['supplier_state'] = 2;
                    $this->save($get_order);
                }
                // 同步购物车
                $UCartdb = new UCartdb();
                $UCartdb->saveCart($this->param['order_no']);
                // 同步发票
                $UInvoicedb = new UInvoicedb();
                $UInvoicedb->saveInvoice($this->param['order_no']);
            }
        } else {
            if ($get_uorder) {
                $get_uorder->save(['delete_time' => time()]);
                //    $get_uorder->save(['delete_time' => time(), 'cancel_type' => 2]);
            }
        }
    }

    public function saveFukuan()
    {


        foreach ($this->param['data'] as $key => $val) {
            $get_order = $this
                ->useGlobalScope(false)
                ->where('out_order_no', $val ['order_no'])
                ->find();
            if ($get_order) {
                $UInvoicedb = new UInvoicedb();
                $get_order = $get_order->toArray();
                $UInvoicedb->useGlobalScope(false)->where('order_nos', $get_order ['order_no'])->update([
                    'order_shoukuan' => $val ['state']
                ]);

                $this->useGlobalScope(false)->where('order_no', $get_order ['order_no'])->update([
                    'pay_state' => $val ['state'],
                    'pay_time' => time()
                ]);
                $UOrderLogdb = new UOrderLogdb();
                // 日志
                $save_log = array(
                    'type_index' => 12,
                    'order_no' => $get_order ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '订单收款',
                    'post_json' => $get_order
                );
                $UOrderLogdb->save($save_log);
            }
        }
    }

    public function saveShoukuan()
    {
        foreach ($this->param['data'] as $key => $val) {
            $get_order = $this
                ->useGlobalScope(false)
                ->where('out_order_no', $val ['order_no'])
                ->find();
            if ($get_order) {
                $get_order->save([
                    'jun_pay_state' => $val['state'],
                    'jun_pay_time' => time()
                ]);
                $UOrderLogdb = new UOrderLogdb();
                // 日志
                $save_log = array(
                    'type_index' => 12,
                    'order_no' => $get_order ['order_no'],
                    'member_id' => MEMBER_ID,
                    'view_text' => '已收客户款',
                    'post_json' => $get_order
                );
                $UOrderLogdb->save($save_log);
            }
        }
    }

    public function setInvoice()
    {
        $order_no = $this->param['order_no'];
        $invoice = $this->param['invoice_data'];
        $get_order = $this->useGlobalScope(false)->where('order_no', $order_no)->find();
        if ($get_order) {
            if ($invoice['state'] == 4) {
                // 发票红冲
                $get_order->save(['invoice_state' => 1, 'returncode' => '红冲成功', 'is_down' => 2]);
            }
            $invoicedb = new UInvoicedb();
            $get_invoice = $invoicedb->where('order_nos', $order_no)->find();
            if ($get_invoice) {
                $get_invoice->save($invoice);
            }
        }

    }

    // 仅设置申请发票状态
    public function applyEinvoiceStateOnly()
    {
        $UCartdb = new UCartdb();
        $get_cart = $UCartdb->where('order_no', $this->param['order_no'])->select();
        if (!$get_cart->isEmpty()) {
            foreach ($get_cart as $key => $val) {
                $val->save(['apply_state' => $this->param['apply_state'], 'nopass_info' => $this->param['nopass_info']]);
            }
        }
        $get_order = $this->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
        if ($get_order) {
            $get_order->save(['apply_state' => $this->param['apply_state'], 'apply_shehe_time' => time(), 'apply_audit_admin_id' => $this->param['apply_audit_admin_id']]);
        }
        //  $save_apply['order_no'] = $this->post['order_no'];
        /*$save_apply['admin_id'] = $this->supplier_id;
        $save_apply['log'] = $this->param['apply_state'] == 3 ? '审核通过' : '驳回审核:' . $this->param['nopass_info'];
        $orderApplyEinvoicedb = new UOrderApplyEinvoicedb();
        $orderApplyEinvoicedb->save($save_apply);*/
    }

    // 申请发票状态
    public function applyEinvoiceState()
    {
        $UCartdb = new UCartdb();
        $data = $this->param['save_data'];
        $get_cart = $UCartdb->where('order_no', $this->param['order_no'])->select();
        foreach ($data as $key => $val) {
            $cart_id = 0;
            foreach ($get_cart as $k => $v) {
                if ($val['goods_sku'] == $v['goods_sku']) {
                    $cart_id = $v['cart_id'];
                }
            }
            if ($get_cart) {
                $save_update['product_area'] = $val['product_area'];
                $save_update['shuiwu_id'] = $val['shuiwu_id'];
                $save_update['product_firm'] = $val['product_firm'];
                $save_update['invoice_model'] = $val['invoice_model'];
                $save_update['goods_keywords'] = $val['goods_keywords'];
                $save_update['goods_unit'] = $val['goods_unit'];
                $save_update['apply_state'] = $this->param['apply_state'];
                $save_update['nopass_info'] = $this->param['nopass_info'];
                $save_update['diy_beizhu'] = $val['diy_beizhu'] ?? '';
                $UCartdb->where('cart_id', $cart_id)->update($save_update);
            }
        }
        $get_order = $this->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
        if ($get_order) {
            $get_order->save(['apply_state' => $this->param['apply_state'], 'apply_shehe_time' => time()]);
        }
        //  $save_apply['order_no'] = $this->post['order_no'];
        /*$save_apply['admin_id'] = $this->supplier_id;
        $save_apply['log'] = $this->param['apply_state'] == 3 ? '审核通过' : '驳回审核:' . $this->param['nopass_info'];
        $orderApplyEinvoicedb = new UOrderApplyEinvoicedb();
        $orderApplyEinvoicedb->save($save_apply);*/
    }

    // 修改客服提醒
    public function editRemark()
    {
        $get_order = $this->useGlobalScope(false)->where('order_no', $this->param['order_no'])->find();
        if ($get_order) {
            $get_order->save($this->param ['post_data']);
        }
    }
}